package com.ruoyi.crop.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Goods;
import com.ruoyi.crop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 产物Controller
 *
 * @author ruoyi
 * @date 2024-07-27
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询产物列表
     */
    @RequiresPermissions("system:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(Goods Goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(Goods);
        return getDataTable(list);
    }

    @GetMapping("/goodsList")
    public AjaxResult goodsList()
    {
        startPage();
        List<Goods> list = goodsService.list();
        return success(list);
    }

    /**
     * 导出产物列表
     */
    @Log(title = "产物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "产物数据");
    }


    /**
     * 获取产物详细信息
     */
    @RequiresPermissions("system:goods:query")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Integer goodsId)
    {
        return success(goodsService.getById(goodsId));
    }

    /**
     * 新增产物
     */
    @RequiresPermissions("system:goods:add")
    @Log(title = "产物", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        goods.setCreateBy(SecurityUtils.getUsername());
        goods.setCreateTime(new Date());
        return toAjax(goodsService.save(goods));
    }

    /**
     * 修改产物
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "产物", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        goods.setUpdateBy(SecurityUtils.getUsername());
        goods.setUpdateTime(new Date());
        return toAjax(goodsService.updateById(goods));
    }

    /**
     * 删除产物
     */
    @RequiresPermissions("system:goods:remove")
    @Log(title = "产物", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Integer[] goodsIds)
    {
        return toAjax(goodsService.removeBatchByIds(Arrays.asList(goodsIds)));
    }
}
